package slamonitor;
import org.apache.axis.AxisFault; 
import org.apache.axis.MessageContext; 
import org.apache.axis.handlers.BasicHandler; 
import org.apache.axis.Message; 
import javax.xml.soap.*; 
import java.io.FileOutputStream; 
import java.io.PrintWriter; 
import java.util.Date; 
import tautils.*; 
public class SLAMonitor extends BasicHandler { 
  private RunningAutomatonInputThroughput raitc; 
  String filename; 
 
  public synchronized void invoke(MessageContext msgContext) throws AxisFault { 
    try { 
      if ( filename == null ) { {

        filename = (String)getOption("filename"); 
  	  System.out.println(filename);
      }
      } 
      if ((filename == null) || (filename.equals(""))) 
        throw new AxisFault("Server.NoLogFile", 
                            "No log file configured for the LogHandler!", 
                              null, null); 
      System.out.println("creo un nuovo file");
      FileOutputStream fos = new FileOutputStream(filename, true); 
      PrintWriter writer = new PrintWriter(fos); 
      FileOutputStream fos1 = new FileOutputStream("/Users/alex/Desktop/SLAMonitorTimeStamp.log", true); 
      PrintWriter writer1 = new PrintWriter(fos1); 
 
      if ( raitc == null ) { 
        raitc = new RunningAutomatonInputThroughput(); 
      } 
 
      Date date = new Date(); 
      long datemillisec = date.getTime(); 
      Message reqmsg = msgContext.getRequestMessage(); 
      Message respmsg = msgContext.getResponseMessage(); 
      if ( respmsg == null ) { 
        // This is a request only. 
         writer1.println(reqmsg + "-- Entry: REQUEST at " + datemillisec); 
         AttachmentPart tStamp = reqmsg.createAttachmentPart(); 
         String contentString1 = String.valueOf(datemillisec); 
         tStamp.setContent(contentString1, "text/plain"); 
         reqmsg.addAttachmentPart(tStamp); 
         raitc.makeTrans(datemillisec, "req",writer); 
         writer1.println(reqmsg + "-- Exit: REQUEST at " + System.currentTimeMillis());      } 
      writer.close(); 
      writer1.close(); 
    } catch (Exception e) { 
      Date t = new Date(); 
      System.out.println("ERROR: " + t + " = " + t.getTime()); 
      e.printStackTrace(); 
      throw AxisFault.makeFault(e); 
    } 
  } 
} 
